package fan.project1_SpeedTalkSystem.Model;

import org.springframework.stereotype.Component;
import org.springframework.util.DigestUtils;

import java.util.UUID;
@Component
public class MD5Utils {

    public static String getSecretPassword(String password) {

        //盐值
        String salt = UUID.randomUUID().toString().replace("-","");
        String s = DigestUtils.md5DigestAsHex((password+salt).getBytes());
        return salt+s;
    }

    public static  boolean checkPassword(String inputPassword,String sqlPassword) {
        if(sqlPassword.length()!=64 || sqlPassword==null)return false;
        String salt=sqlPassword.substring(0,32);
        String d5 =sqlPassword.substring(32);
        String fin = salt+DigestUtils.md5DigestAsHex((inputPassword + salt).getBytes());
        return (fin).equals(sqlPassword);
    }
    //测试方法
    public static void main(String[] args) {
        System.out.println(getSecretPassword("root"));
        System.out.println(checkPassword("123456",getSecretPassword("123456")));


    }
}
